/*
 * @Author: silence
 * @Date: 2022-08-02 09:13:34
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-08-03 12:31:07
 * @Description: 底部Sheet组件
 */

import 'package:flutter/material.dart';
import '../../widgets/TextView.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyBottomSheetWidget extends StatefulWidget {
  /// 菜单列表
  final List<String>? itemList;

  /// 警告/删除
  final String? warnTitle;

  /// item的高度，默认54
  final double? itemHeight;

  /// 选中了第几个
  final void Function(int selectedIndex)? tapSureSeletedFunc;

  /// 点击了警告/删除
  final void Function()? tapWarnFunc;

  /// content 自定义内容
  final Widget? contentWidget;

  const MyBottomSheetWidget(
      {this.itemList,
      this.warnTitle,
      this.itemHeight,
      this.tapSureSeletedFunc,
      this.tapWarnFunc,
      this.contentWidget,
      Key? key})
      : super(key: key);

  @override
  State<MyBottomSheetWidget> createState() => _MyBottomSheetWidgetState();
}

class _MyBottomSheetWidgetState extends State<MyBottomSheetWidget> {
  late double _sheetItemHeight;
  late List<String> _items;

  @override
  void initState() {
    super.initState();

    _sheetItemHeight = widget.itemHeight ?? 54.w;
    _items = widget.itemList ?? [];

    if (widget.warnTitle != null) {
      _items.add(widget.warnTitle!);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Container(
            decoration: BoxDecoration(
                borderRadius: BorderRadius.horizontal(
                    left: Radius.circular(16.w), right: Radius.circular(16.w)),
                color: Colors.white),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                widget.contentWidget ??
                    Container(
                      height: _sheetItemHeight * (_items.length),
                      child: ListView.separated(
                          itemBuilder: (context, index) {
                            return itemWidget(index);
                          },
                          separatorBuilder: (context, index) => Container(
                                color: Color.fromRGBO(0, 0, 0, 0.1),
                                height: 0.5.w,
                              ),
                          itemCount: _items.length),
                    ),
                // 中间大大的分隔线
                Container(
                  height: 12.w,
                  color: Color.fromRGBO(248, 248, 250, 1),
                ),
                // 底部取消按钮
                TextView(
                  '取消',
                  height:
                      _sheetItemHeight + MediaQuery.of(context).padding.bottom,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.sp,
                  ),
                ),
              ],
            )));
  }

  // 单项item组件
  Widget itemWidget(int index) {
    String itemValue = _items[index];

    bool curIsWarn = false; // 当前是不是警告/删除
    if (widget.warnTitle != null && itemValue == widget.warnTitle!) {
      curIsWarn = true;
    }

    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        // 删除
        if (curIsWarn && widget.tapWarnFunc != null) {
          widget.tapWarnFunc!();
          return;
        }

        // 普通
        if (widget.tapSureSeletedFunc != null) {
          widget.tapSureSeletedFunc!(index);
          return;
        }

        Navigator.pop(context);
      },
      child: Container(
        alignment: Alignment.center,
        height: _sheetItemHeight,
        child: TextView(
          itemValue,
          style: TextStyle(
            color: curIsWarn
                ? const Color.fromRGBO(254, 59, 48, 1)
                : const Color.fromRGBO(48, 49, 51, 1),
            fontSize: 16.sp,
          ),
        ),
      ),
    );
  }
}
